package com.fbee.smartcommunities.controller;

import com.fbee.smartcommunities.entity.Shop;
import com.fbee.smartcommunities.entity.User;
import com.fbee.smartcommunities.service.ShopService;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.vo.ResultVO;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Resource
    private ShopService shopService;

    @Resource
    protected UserService userService;

    @PostMapping("/create")
    ResultVO<Boolean> createShop(Long openId){
        User user = userService.getById(openId);
        return ResultVO.success(shopService.save(new Shop(openId.toString(),0,null,openId,null,user.getAddress(),user.getPhone())));
    }

    @GetMapping("/getShopInfoById")
    ResultVO<Shop> getShopInfoByUid(Long openId){
        return ResultVO.success(shopService.getShopInfoByUid(openId));
    }

    @PostMapping("/update")
    ResultVO<Boolean> updateShopInfo(Long id,String name, String description, String address, String phone, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(shopService.updateShopInfo(id,name,description,address,phone,file),"修改成功");
    }
}
